package com.example.workrecord.service;

import com.example.workrecord.dao.BaseJPA;
import com.example.workrecord.dao.PlatFormbDao;
import com.example.workrecord.entity.PlatFormb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlatFormbService extends BaseService<PlatFormb, Long> {
    @Autowired
    private PlatFormbDao dao;

    @Override
    protected BaseJPA<PlatFormb> getDao() {
        return dao;
    }

    public void deleteByPlatForm(Long id) {
        List<PlatFormb> list=dao.findAllByPlatformId(id);
        if (null == list) {
            return;
        }
        for (PlatFormb formb : list) {
            delete(formb.getId());
        }
    }

    public void editByform(List<PlatFormb> platFormbs, Long id) {
        List<PlatFormb> list=dao.findAllByPlatformId(id);
        if (null == list) {
            saveAll(platFormbs);
            return;
        }
        List<Long> longs=new ArrayList<>();
        for (PlatFormb formb : platFormbs) {
            if (null == formb.getId() || formb.getId().equals("")) {
                formb.setPlatformId(id);
                save(formb);
                continue;
            }
            update(formb.getId(), formb);
            longs.add(formb.getId());
        }

        for (PlatFormb platFormb : list) {
            if(!longs.contains(platFormb.getId())){
                delete(platFormb.getId());
            }
        }

    }

    public List<PlatFormb> findByPlatId(Long id) {
        return dao.findAllByPlatformId(id);
    }
}
